/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: syyyy
 * Date: 2025-11-03
 * Time: 20:52
 */
public class Test {
    public static void dfs(char[][] grid,int i,int j){
        int n= grid.length;
        int m=grid[0].length;
        grid[i][j]='0';
        if(i-1>=0 && grid[i-1][j]=='1'){
            dfs(grid,i-1,j);
        }
        if(i+1<n && grid[i+1][j]=='1'){
            dfs(grid,i+1,j);
        }
        if(j-1>=0 && grid[i][j-1]=='1'){
            dfs(grid,i,j-1);
        }
        if(j+1>=0 && grid[i][j+1]=='1'){
            dfs(grid,i,j+1);
        }
    }
    public static int solve(char[][] grid){
        int n= grid.length;
        if(n==0){
            return 0;
        }
        int m=grid[0].length;
        int count=0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(grid[i][j]=='1'){
                    count++;
                    dfs(grid,i,j);
                }
            }
        }
        return count;
    }
    public static void main(String[] args) {

    }
}
